#ifndef __USER_DAO_H__
#define __USER_DAO_H__

#include "MysqlDaoBase.h"

class UserDao : public MysqlDaoBase 
{
public:
    using MysqlDaoBase::MysqlDaoBase;

    bool InsertUser(const std::string& name, const std::string& email, const std::string& pwd) {
        std::string sql = "INSERT INTO user(name, email, pwd) VALUES('" 
                        + name + "','" + email + "','" + pwd + "')";
        return ExecUpdate(sql) > 0;
    }

    // 根据用户名更新密码
    bool UpdatePasswordByName(const std::string& name, const std::string& newPwd) {
        std::string sql = "UPDATE user SET pwd='" + newPwd + "' WHERE name='" + name + "'";
        return ExecUpdate(sql) > 0;
    }

    // 获取所有用户信息
    std::vector<std::map<std::string,std::string>> GetAllUsers() {
        return ExecQuery("SELECT id, name, email, pwd FROM user");
    }

    // 根据邮箱获取用户信息
    std::map<std::string, std::string> GetUserByEmail(const std::string& email) {
        auto results = ExecQuery("SELECT id, name, email, pwd FROM user WHERE email='" + email + "'");
        if (results.empty()) return {};
        return results[0];
    }

    // 根据用户名获取用户信息
    std::map<std::string, std::string> GetUserByName(const std::string& name) {
        auto results = ExecQuery("SELECT id, name, email FROM user WHERE name='" + name + "'");
        if (results.empty()) return {};
        return results[0];
    }

    // 根据用户ID获取用户信息
    std::map<std::string, std::string> GetUserById(const std::string& id) {
        auto results = ExecQuery("SELECT id, name, email FROM user WHERE id='" + id + "'");
        if (results.empty()) return {};
        return results[0];
    }
};


#endif // !__USER_DAO_H__